import axios from "axios";

const request = axios.create({
  widthCredentials: true,
  timeout: 30000,
});

const baseUrl = "https://red.library.sh.cn/service_redsource";
const isPro = Object.is(process.env.NODE_ENV, "production");

request.interceptors.request.use((req) => {
  if (isPro) {
    req.url = baseUrl + req.url;
  }
  req.headers.token =
    "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJhdXRoMCIsInVzZXJQd2QiOiI2ODNlYjYwOTYwN2E0MzliMDU2MWRjYmI0YzgzMjllOGEiLCJ1c2VyTmFtZSI6ImRlYmIxNjM3N2RjYTU5YzA3Y2M1OGI4Nzk0ZmZhYTdiYSJ9.spCAcUAHDnCX9XXr24PUeUVXbve1OTCunRi44UEt2yc";
  return req;
});

request.interceptors.response.use(
  (res) => {
    return res.data;
  },
  (error) => {
    return {
      flag: false,
      data: error,
    };
  }
);

export default request;
